﻿using EasyInvoice.WPF.ViewModel;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasyInvoice.WPF.Common
{
    public class InvoiceDocument
    {
        private InvoiceViewModel _invoice;

        public InvoiceDocument(InvoiceViewModel invoice, string filepath)
        {
            this._invoice = invoice;

            using (Document doc = new Document(PageSize.LETTER, 72, 36, 72, 72))
            {
                PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(filepath, FileMode.Create));
                doc.Open();
                doc.NewPage();

                doc.Add(_sellerTable(this._invoice.Seller));

                doc.Add(new Paragraph(" "));
                doc.Add(new Paragraph(" "));

                Paragraph pTitle = new Paragraph("INVOICE", new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 16, 1));
                pTitle.Alignment = 1;
                doc.Add(pTitle);

                doc.Add(new Paragraph(" "));
                doc.Add(new Paragraph(" "));

                doc.Add(this._invoiceTable());

                doc.Add(new Paragraph(" "));

                doc.Add(this._clientTable(this._invoice.Client));

                doc.Add(_detailsTable(this._invoice.Details.ToList()));

                doc.Add(new Paragraph(" "));
                doc.Add(new Paragraph(" "));

                doc.Add(new Paragraph(" "));
                doc.Add(new Paragraph(" "));

                doc.Add(new Paragraph(" "));
                doc.Add(new Paragraph(" "));
                
                doc.Add(new Paragraph(" "));
                doc.Add(new Paragraph(" "));


                doc.Add(this._signatureTable(this._invoice.Seller));
            }
        }

        private PdfPTable _sellerTable(ClientViewModel seller)
        {
            PdfPTable header = new PdfPTable(1);
            // header.TotalWidth = RECT.Width;
            header.WidthPercentage = 100;

            //PdfPCell sellerFrom = new PdfPCell(new Phrase("From:", new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.COURIER, 12)));
            //sellerFrom.Border = iTextSharp.text.Rectangle.NO_BORDER;
            //sellerFrom.VerticalAlignment = Element.ALIGN_TOP;
            //sellerFrom.HorizontalAlignment = Element.ALIGN_LEFT;
            //header.AddCell(sellerFrom);



            PdfPCell sellerNameCell = new PdfPCell(new Phrase(seller.Name, new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12, 1)));
            sellerNameCell.Border = iTextSharp.text.Rectangle.NO_BORDER;
            sellerNameCell.VerticalAlignment = Element.ALIGN_TOP;
            sellerNameCell.HorizontalAlignment = Element.ALIGN_LEFT;
            header.AddCell(sellerNameCell);

            PdfPCell sellerAddress = new PdfPCell(new Phrase(seller.Address + ", " + seller.City + ", " + seller.Country, new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12)));
            sellerAddress.Border = iTextSharp.text.Rectangle.NO_BORDER;
            sellerAddress.VerticalAlignment = Element.ALIGN_TOP;
            sellerAddress.HorizontalAlignment = Element.ALIGN_LEFT;
            header.AddCell(sellerAddress);

            PdfPCell sellerCUI = new PdfPCell(new Phrase(" ", new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12)));//PdfPCell sellerCUI = new PdfPCell(new Phrase("CIF: 22053995", new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12)));
            sellerCUI.Border = iTextSharp.text.Rectangle.NO_BORDER;
            sellerCUI.VerticalAlignment = Element.ALIGN_TOP;
            sellerCUI.HorizontalAlignment = Element.ALIGN_LEFT;
            header.AddCell(sellerCUI);
            PdfPCell sellerRegCom = new PdfPCell(new Phrase(" ", new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12)));//PdfPCell sellerRegCom = new PdfPCell(new Phrase("Nr.ord.Reg.Com: F32/458/03.07.2007", new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12)));
            sellerRegCom.Border = iTextSharp.text.Rectangle.NO_BORDER;
            sellerRegCom.VerticalAlignment = Element.ALIGN_TOP;
            sellerRegCom.HorizontalAlignment = Element.ALIGN_LEFT;
            header.AddCell(sellerRegCom);


            PdfPCell receiverCUI = new PdfPCell(new Phrase("", new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12)));
            receiverCUI.Border = iTextSharp.text.Rectangle.NO_BORDER;
            receiverCUI.VerticalAlignment = Element.ALIGN_TOP;
            receiverCUI.HorizontalAlignment = Element.ALIGN_LEFT;
            header.AddCell(receiverCUI);

            return header;
        }

        private PdfPTable _clientTable(ClientViewModel client)
        {
            PdfPTable table = new PdfPTable(1);
            //table.TotalWidth = RECT.Width/2;
            table.WidthPercentage = 50;
            table.HorizontalAlignment = 0;

            PdfPCell tableFrom = new PdfPCell(new Phrase("To:", new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12)));
            tableFrom.Border = iTextSharp.text.Rectangle.NO_BORDER;
            tableFrom.VerticalAlignment = Element.ALIGN_TOP;
            tableFrom.HorizontalAlignment = Element.ALIGN_LEFT;
            table.AddCell(tableFrom);

            PdfPCell tableNameCell = new PdfPCell(new Phrase(client.Name, new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12, 1)));
            tableNameCell.Border = iTextSharp.text.Rectangle.NO_BORDER;
            tableNameCell.VerticalAlignment = Element.ALIGN_TOP;
            tableNameCell.HorizontalAlignment = Element.ALIGN_LEFT;
            table.AddCell(tableNameCell);

            PdfPCell tableAddress = new PdfPCell(new Phrase(String.Format("{0}, {1}, {2},{3},{4}", client.Address, client.PostalCode, client.City, client.State, client.Country), new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12)));
            tableAddress.Border = iTextSharp.text.Rectangle.NO_BORDER;
            tableAddress.VerticalAlignment = Element.ALIGN_TOP;
            tableAddress.HorizontalAlignment = Element.ALIGN_LEFT;
            table.AddCell(tableAddress);

            return table;
        }

        private PdfPTable _invoiceTable()
        {
            PdfPTable table = new PdfPTable(2);
            //invoice.TotalWidth = page.Width/2;
            table.WidthPercentage = 50;

            PdfPCell invoiceNoCell = new PdfPCell(new Phrase("Invoice #:", new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12, 1)));
            invoiceNoCell.Border = iTextSharp.text.Rectangle.NO_BORDER;
            invoiceNoCell.VerticalAlignment = Element.ALIGN_TOP;
            invoiceNoCell.HorizontalAlignment = Element.ALIGN_RIGHT;
            table.AddCell(invoiceNoCell);

            PdfPCell invoiceNo = new PdfPCell(new Phrase(this._invoice.Number, new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12, 1)));
            invoiceNo.Border = iTextSharp.text.Rectangle.NO_BORDER;
            invoiceNo.VerticalAlignment = Element.ALIGN_TOP;
            invoiceNo.HorizontalAlignment = Element.ALIGN_LEFT;
            table.AddCell(invoiceNo);

            PdfPCell invoiceDateCell = new PdfPCell(new Phrase("Date:", new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12,1)));
            invoiceDateCell.Border = iTextSharp.text.Rectangle.NO_BORDER;
            invoiceDateCell.VerticalAlignment = Element.ALIGN_TOP;
            invoiceDateCell.HorizontalAlignment = Element.ALIGN_RIGHT;
            table.AddCell(invoiceDateCell);

            PdfPCell invoiceDate = new PdfPCell(new Phrase(this._invoice.Date.ToString("MMMM dd, yyyy"), new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12,1)));
            invoiceDate.Border = iTextSharp.text.Rectangle.NO_BORDER;
            invoiceDate.VerticalAlignment = Element.ALIGN_TOP;
            invoiceDate.HorizontalAlignment = Element.ALIGN_LEFT;
            table.AddCell(invoiceDate);

            return table;
        }

        private PdfPTable _detailsTable(List<InvoiceDetailViewModel> details)
        {

            string[] col = { "No.", "Description", "Unit Price (USD)", "Quantity", "Amount (USD)" };
            PdfPTable table = new PdfPTable(5);
            table.WidthPercentage = 100;
           
            // then set the column's __relative__ widths
            table.SetWidths(new Single[] { 1, 4, 2, 1, 2 });
            table.SpacingBefore = 10;
            for (int i = 0; i < col.Length; ++i)
            {
                PdfPCell cell = new PdfPCell(new Phrase(col[i]));
                cell.BackgroundColor = new BaseColor(204, 204, 204);
                cell.Border = 0;

                table.AddCell(cell); 
            }
           
            foreach (InvoiceDetailViewModel detail in details)
            {

                PdfPCell detailIndex = new PdfPCell(
                            new Phrase(detail.Index.ToString(), 
                                new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12)));
                detailIndex.Border = iTextSharp.text.Rectangle.BOTTOM_BORDER;
                detailIndex.VerticalAlignment = Element.ALIGN_TOP;
                detailIndex.HorizontalAlignment = Element.ALIGN_LEFT;
                table.AddCell(detailIndex);

                PdfPCell detailDescription = new PdfPCell(
                                            new Phrase(detail.Description,
                                            new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12)));
                detailDescription.Border = iTextSharp.text.Rectangle.BOTTOM_BORDER;
                detailDescription.VerticalAlignment = Element.ALIGN_TOP;
                detailDescription.HorizontalAlignment = Element.ALIGN_LEFT;
                table.AddCell(detailDescription);

                PdfPCell detailPrice = new PdfPCell(
                                            new Phrase("$" + detail.Price.ToString("f2"),
                                            new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12)));
                detailPrice.Border = iTextSharp.text.Rectangle.BOTTOM_BORDER;
                detailPrice.VerticalAlignment = Element.ALIGN_TOP;
                detailPrice.HorizontalAlignment = Element.ALIGN_RIGHT;
                table.AddCell(detailPrice);

                PdfPCell detailUnits = new PdfPCell(
                                            new Phrase(detail.Units.ToString(),
                                            new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12)));
                detailUnits.Border = iTextSharp.text.Rectangle.BOTTOM_BORDER;
                detailUnits.VerticalAlignment = Element.ALIGN_TOP;
                detailUnits.HorizontalAlignment = Element.ALIGN_RIGHT;
                table.AddCell(detailUnits);

                PdfPCell detailValue = new PdfPCell(
                                            new Phrase("$" + detail.Value.ToString("f2"),
                                            new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12)));
                detailValue.Border = iTextSharp.text.Rectangle.BOTTOM_BORDER;
                detailValue.VerticalAlignment = Element.ALIGN_TOP;
                detailValue.HorizontalAlignment = Element.ALIGN_RIGHT;
                table.AddCell(detailValue);

                //table.AddCell(detail.Index.ToString());
                //table.AddCell(detail.Description);
                //table.AddCell("$" + detail.Price.ToString("f2"));
                //table.AddCell(detail.Units.ToString());
                //table.AddCell("$" + detail.Value.ToString("f2"));
            }
            PdfPCell total = new PdfPCell(new Phrase("Total Invoice Amount (USD):", new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12, 1)));
            total.Colspan = 4;
            total.Border = iTextSharp.text.Rectangle.BOTTOM_BORDER;
            total.HorizontalAlignment = Element.ALIGN_RIGHT;
            table.AddCell(total);

            PdfPCell totalValue = new PdfPCell(new Phrase("$" + _invoice.TotalAmount.ToString("f2"), new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12, 1)));
            totalValue.Border = iTextSharp.text.Rectangle.BOTTOM_BORDER;
            totalValue.HorizontalAlignment = Element.ALIGN_RIGHT;
            table.AddCell(totalValue);


            //Image gif = Image.GetInstance(Bitmaps.byteArrayToImage(this._invoice.Seller.Signature), System.Drawing.Imaging.ImageFormat.Png);
            //PdfPCell signature = new PdfPCell(new Phrase("Signature"));
            //signature.Colspan = 4;
            //signature.HorizontalAlignment = Element.ALIGN_RIGHT;
            //signature.VerticalAlignment = Element.ALIGN_MIDDLE;
            //table.AddCell(signature);
            //table.AddCell(gif);

            return table;
        }

        private PdfPTable _signatureTable(ClientViewModel seller)
        {
            PdfPTable table = new PdfPTable(1);
            //table.TotalWidth = RECT.Width/2;
            table.WidthPercentage = 30;
            //table.TotalWidth = 100;
            table.HorizontalAlignment = Element.ALIGN_LEFT;

            PdfPCell signatureText = new PdfPCell(new Phrase("Authorized Signature", new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12)));
            signatureText.Border = iTextSharp.text.Rectangle.NO_BORDER;
            signatureText.VerticalAlignment = Element.ALIGN_TOP;
            signatureText.HorizontalAlignment = Element.ALIGN_LEFT;
            table.AddCell(signatureText);

            Image signaturePNG = Image.GetInstance(Bitmaps.byteArrayToImage(seller.Signature), System.Drawing.Imaging.ImageFormat.Png);
            signaturePNG.ScalePercent(15f);

            PdfPCell signatureImage = new PdfPCell(signaturePNG);
            signatureImage.Border = iTextSharp.text.Rectangle.BOTTOM_BORDER;
            signatureImage.VerticalAlignment = Element.ALIGN_TOP;
            signatureImage.HorizontalAlignment = Element.ALIGN_LEFT;
            table.AddCell(signatureImage);

            return table;
        }

    }
}
